package siedler.GUI;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import siedler.communication.PeerI;



public class _startSiedlerGui_ {

	public static int s = 600;
	public static int frameSizeX=s, frameSizeY=s;
	
	public PeerI peer;
	
	/**
	 * @param args
	 */
//	public static void main(String[] args) {
//		SwingUtilities.invokeLater(new Runnable() {
//			public void run() {
//				initGui();
//			}
//		});
//	}
	
	/**
	 * initGui
	 */
	public void initGui(PeerI p) {
		
		peer = p;
		
		// --- init frame ---
		//
		JFrame frame = makeFrame();
		
		// --- make game panel ---
		//
		SiedlerGamePanel gameP = makeGamePanel();
		
		// --- make button panel ---
		//
		JPanel buttonPanel = getButtonPanel(gameP);
		
		// --- make tabbed pane ---
		//
		JTabbedPane tabbedP = new JTabbedPane();
		tabbedP.addTab(gameP.getName(), gameP);		// add the drawing panel to the tabs
		//tabbedP.addTab("Nothing", new JPanel());	// add another panel to the tabs
		
		// --- add to frame (frame-standard is border layout) ---
		//
		frame.add(tabbedP, BorderLayout.CENTER);
		frame.add(buttonPanel, BorderLayout.SOUTH);
		
		// --- last configs and showing Gui ---
		//
		gameP.requestFocus();				// for keyListener to work
		frame.setVisible(true);				// ___ show GUI ___
		//gameP.initGraphics();				// calculate the size an position of the elements
	}
	
	/**
	 * makeFrame
	 * @return main frame
	 */
	private static JFrame makeFrame(){
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// f.setSize(Toolkit.getDefaultToolkit().getScreenSize());
		frame.setSize(frameSizeX, frameSizeY);
		frame.setBackground(Color.BLACK);
		frame.setJMenuBar(makeMenue());
		frame.setTitle("Siedler");
		return frame;
	}
	
	/**
	 * getButtonPanel
	 * @param gameP - action listener
	 * @return JPanel buttonPanel
	 */
	private static JPanel getButtonPanel(SiedlerGamePanel gameP){
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setBackground(Color.LIGHT_GRAY);
		//buttonPanel.setLayout(new FlowLayout(FlowLayout.));
		//buttonPanel.setMaximumSize(new Dimension(100, 100));
		
		JButton bLine = new JButton("End Move");		// button for drawing lines
		bLine.addActionListener(gameP);				// action listener
		bLine.setFocusable(false);					// not focusable - to not stealing the focus for key input
		buttonPanel.add(bLine);						// add button to panel
		
//		JButton bCicle = new JButton("Button 1");		// -"-
//		bCicle.addActionListener(gameP);			//-
//		bCicle.setFocusable(false);					//-
//		buttonPanel.add(bCicle);					//-
//		
//		JButton bString = new JButton("Button 2");	// -"-
//		bString.addActionListener(gameP);			//-
//		bString.setFocusable(false);				//-
//		buttonPanel.add(bString);					//-
//		
//		JButton bImage = new JButton("Button 3");		// -"-
//		bImage.addActionListener(gameP);			//-
//		bImage.setFocusable(false);					//-
//		buttonPanel.add(bImage);					//-
//		
//		JButton bNothing = new JButton("Nothing");	// -"-
//		bNothing.addActionListener(gameP);			//-
//		bNothing.setFocusable(false);				//-
//		buttonPanel.add(bNothing);					//-
		
		return buttonPanel;
	}
	
	/**
	 * makeGamePanel
	 * @return Siedler_v1 gemePanel
	 * @throws RemoteException 
	 */
	private SiedlerGamePanel makeGamePanel(){
		
		SiedlerGamePanel gameP = null;
		try {
			gameP = new SiedlerGamePanel("Player " +peer.getId()+" GamePanel", peer);
			gameP.setBackground(Color.ORANGE);
			gameP.addKeyListener(gameP);
			gameP.addMouseListener(gameP);
			gameP.addComponentListener(gameP);
			
			
		} catch (RemoteException e) {
			e.printStackTrace();
			
		}
		return gameP;
		
	}
	
	/**
	 * makeMenue
	 * @return JMenuBar - a menu for the JFrame. (f.setJMenuBar(...))
	 */
	private static JMenuBar makeMenue(){
		
		//Where the GUI is created:
		JMenuBar menuBar;
		JMenu menu, submenu;
		JMenuItem menuItem;
		JRadioButtonMenuItem rbMenuItem;
		JCheckBoxMenuItem cbMenuItem;

		//Create the menu bar.
		menuBar = new JMenuBar();

		//Build the first menu.
//		menu = new JMenu("A Menu");
//		menu.setMnemonic(KeyEvent.VK_A);
//		menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
//		menuBar.add(menu);

		//a group of JMenuItems
//		menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
//		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
//		menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
//		menu.add(menuItem);
//
//		menuItem = new JMenuItem("Both text and icon", new ImageIcon("images/middle.gif"));
//		menuItem.setMnemonic(KeyEvent.VK_B);
//		menu.add(menuItem);
//
//		menuItem = new JMenuItem(new ImageIcon("images/middle.gif"));
//		menuItem.setMnemonic(KeyEvent.VK_D);
//		menu.add(menuItem);
//
//		//a group of radio button menu items
//		menu.addSeparator();
//		ButtonGroup group = new ButtonGroup();
//		rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
//		rbMenuItem.setSelected(true);
//		rbMenuItem.setMnemonic(KeyEvent.VK_R);
//		group.add(rbMenuItem);
//		menu.add(rbMenuItem);
//
//		rbMenuItem = new JRadioButtonMenuItem("Another one");
//		rbMenuItem.setMnemonic(KeyEvent.VK_O);
//		group.add(rbMenuItem);
//		menu.add(rbMenuItem);
//
//		//a group of check box menu items
//		menu.addSeparator();
//		cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
//		cbMenuItem.setMnemonic(KeyEvent.VK_C);
//		menu.add(cbMenuItem);
//
//		cbMenuItem = new JCheckBoxMenuItem("Another one");
//		cbMenuItem.setMnemonic(KeyEvent.VK_H);
//		menu.add(cbMenuItem);
//
//		//a submenu
//		menu.addSeparator();
//		submenu = new JMenu("A submenu");
//		submenu.setMnemonic(KeyEvent.VK_S);
//
//		menuItem = new JMenuItem("An item in the submenu");
//		menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
//		submenu.add(menuItem);
//
//		menuItem = new JMenuItem("Another item");
//		submenu.add(menuItem);
//		menu.add(submenu);
//
//		//Build second menu in the menu bar.
//		menu = new JMenu("Another Menu");
//		menu.setMnemonic(KeyEvent.VK_N);
//		menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
//		menuBar.add(menu);

		return menuBar;
	}
}
